import cv2
import os
import pandas as pd

txt_folder_path = r'D:\zxd\tf2.2\yolov5\logs\detect_all\detection-results/' #txt path
image_path = r'D:\zxd\tf2.2\yolov5\VOCdevkit\VOC2007\JPEGImages/'          #jpg path
images_save = r'D:\zxd\tf2.2\yolov5\logs\detect_all\images-optional/'       #jpg save path
txt_names = os.listdir(txt_folder_path)                                    
for txt_name in txt_names: 

    image = cv2.imread(image_path+txt_name[0:-4]+'.jpg')
    txt_file=pd.read_csv(txt_folder_path + txt_name)
    i=1

    with open(txt_folder_path + txt_name, "r") as f:
        
        for line in f.readlines():
            line = line.split()
            if float(line[1]) > 0.5 :
                xmin = line[2]
                ymin = line[3]
                xmax = line[4]
                ymax = line[5]
                image = cv2.rectangle(image, (int(xmin),int(ymin)), (int(xmax),int(ymax)), (0, 0, 255), 2)
    cv2.imwrite(images_save+txt_name[0:-4]+'.jpg', image)
    print(txt_name)
